package com.wqz.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wqz.pojo.Commodity;
import com.wqz.service.CommodityService;
import com.wqz.utils.MessageJson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RequestMapping("/commodity")
@Controller
public class CommodityController {

    //@Autowired
    //@Qualifier("commodityServiceImpl")
    @Resource
    private CommodityService commodityService;


    @RequestMapping("/findCommodityList")     //@RequestMapping 映射加载路径
    @ResponseBody
    public List<Commodity> findCommodityList(){
        List<Commodity> commodityList =null;
        try {
            commodityList = commodityService.findCommodityList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return commodityList;
    }

    //@ResponseBody
    //@RequestMapping("/addCommodity")     //@RequestMapping 映射加载路径

    @RequestMapping("/addCommodity")
    @ResponseBody
    public MessageJson addCommodity(Commodity commodity, @RequestBody String jsonDate) {

        //System.out.println("jsonDate = " + jsonDate);
        JSONObject jsonObject = JSON.parseObject(jsonDate);
        //System.out.println(jsonObject.getString("name"));
        //System.out.println("------------------+++++");
        try {
            /*
            if (file != null){
                // 原始文件名
                String originalFileName = file.getOriginalFilename();
                // 获取图片后缀
                String suffix = originalFileName.substring(originalFileName.lastIndexOf("."));
                // 生成图片存储的名称，UUID 避免相同图片名冲突，并加上图片后缀
                String fileName = UUID.randomUUID().toString() + suffix;
                // 图片存储路径
                String filePath = Constants.IMG_PATH + fileName;
                File saveFile = new File(filePath);
                // 将上传的文件保存到服务器文件系统
                file.transferTo(saveFile);
                // 记录服务器文件系统图片名称
                commodity.setPicture(filePath);
            }
            */

            //String name = request.getParameter("name");
            //System.out.println("name = "+name);
            String name = jsonObject.getString("name");
            if(name!=""&&name!=null){
                commodity.setName(name);
            }
            //String type = request.getParameter("type");
            //System.out.println("type = " + type);
            String type = jsonObject.getString("type");
            if(type!=""&&type!=null){
                commodity.setType(type);
            }
            //String price = request.getParameter("price");
            //System.out.println("price = " + price);
            String price = jsonObject.getString("price");
            if(price!=""&&price!=null){
                commodity.setPrice(Double.parseDouble(price));
            }
            //String publish_time = request.getParameter("publish_time");
            String publish_time = jsonObject.getString("publish_time");
            if(publish_time!=""&&publish_time!=null){
                commodity.setPublish_time(publish_time);
            }
            //String description = request.getParameter("description");
            String description = jsonObject.getString("description");
            if(description!=""&&description!=null){
                commodity.setDescription(description);
            }
            int a = commodityService.addCommodity(commodity);
            if(a==1){
                return new MessageJson("200","注册成功");
            }else{
                return new MessageJson("201","注册失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new MessageJson("202","系统异常");
        }

    }
    @RequestMapping("/findSomeCommodity")
    @ResponseBody
    public List<Commodity> addSomeCommodity(Commodity commodity, @RequestBody String jsonDate) {

        JSONObject jsonObject = JSON.parseObject(jsonDate);
        List<Commodity> commodityList = null;
        try {
            String keyWord = jsonObject.getString("keyWord");
            System.out.println("+++++++++++++++++++keyWord = " + keyWord);
            commodityList = commodityService.findSomeCommodity(keyWord);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return commodityList;
    }







    @RequestMapping("/deleteCommodity")     //@RequestMapping 映射加载路径
    public String deleteCommodity(HttpServletRequest request){
        try {
            String id=request.getParameter("id");
            Integer id1= Integer.parseInt(id);
            //Commodity commodity =commodityService.findCommodityById(id1);
            //String pic = commodity.getPicture();
            //System.out.println(new File(pic).delete());
            int a=commodityService.deleteCommodity(id1);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "zhuye";
    }
}
